﻿/*
BF4Rcon.NET, provides a .NET implementation of BF4's RCON interface.
Copyright (C) 2013 Tim Marro

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using TimScripts.BF4Rcon.Frostbite;

namespace TimScripts.BF4Rcon.Test
{
    [TestFixture]
    public class PacketConverterTests
    {
        PacketConverter Converter;
        Queue<byte[]> DataToAdd;
        bool _DataRequested;

        private bool DataRequested
        {
            get
            {
                //i feel like im forgetting a better way :D
                //toggle
                return !(_DataRequested = !_DataRequested);
            }
            set
            {
                _DataRequested = value;
            }
        }

        [SetUp]
        public void SetUp()
        {
            Converter = new PacketConverter(RequestDataCallback, RequestDataCallbackAsync);
            DataToAdd = new Queue<byte[]>();
            _DataRequested = false;
        }

        [Test]
        public void GetPacketBytesReturnsCorrectBytes()
        {
            Packet packet = new Packet(true, false, Packet.MaxSequenceValue, "AAAA");
            BF3Packet oldPacket = packet.ToPacket();

            byte[] actual = Converter.GetPacketBytes(packet);

            byte[] expected = oldPacket.ToByteArray();

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void GetPacketReturnsPacketWhenAllDataPresent()
        {
            Packet expected = new Packet(true, false, 1, "a");
            Packet actual;

            AddPacketToQueue(expected);

            actual = Converter.GetPacket();

            Assert.True(expected.PacketEquals(actual));
        }

        [Test]
        public async Task GetPacketReturnsPacketWhenAllDataPresentAsync()
        {
            Packet expected = new Packet(true, false, 1, "a");
            Packet actual;

            AddPacketToQueue(expected);

            actual = await Converter.GetPacketAsync();

            Assert.True(expected.PacketEquals(actual));
        }

        [Test]
        public void MultiplePacketsReadFromOneBlockOfData()
        {
            Packet packet1 = new Packet(true, false, 1, "a");
            Packet packet2 = new Packet(false, true, 1337, "q");
            Packet actual1, actual2;

            byte[] bytes1 = Converter.GetPacketBytes(packet1);
            byte[] bytes2 = Converter.GetPacketBytes(packet2);

            List<byte> block = new List<byte>(bytes1.Length + bytes2.Length);
            block.AddRange(bytes1);
            block.AddRange(bytes2);
            DataToAdd.Enqueue(block.ToArray());

            actual1 = Converter.GetPacket();
            actual2 = Converter.GetPacket();

            Assert.True(packet1.PacketEquals(actual1));
            Assert.True(packet2.PacketEquals(actual2));
        }

        [Test]
        public void SplitUpPacketCanBeRead()
        {
            Packet packet = new Packet(true, false, 1, "a");
            Packet actual;
            byte[] packetBytes = Converter.GetPacketBytes(packet);
            byte[] part1, part2;
            int mid = packetBytes.Length / 2;

            part1 = packetBytes.Take(mid).ToArray();
            part2 = packetBytes.Skip(mid).ToArray();

            DataToAdd.Enqueue(part1);
            DataToAdd.Enqueue(part2);

            actual = Converter.GetPacket();

            Assert.True(packet.PacketEquals(actual));
        }

        private void AddPacketToQueue(Packet packet)
        {
            DataToAdd.Enqueue(Converter.GetPacketBytes(packet));
        }

        private IEnumerable<byte> RequestDataCallback()
        {
            DataRequested = true;

            if (DataToAdd.Count == 0)
                return null;

            return DataToAdd.Dequeue();
        }

        private async Task<IEnumerable<byte>> RequestDataCallbackAsync()
        {
            DataRequested = true;

            if (DataToAdd.Count == 0)
                return null;

            return DataToAdd.Dequeue();
        }
    }
}
